const Sequelize = require('sequelize')
const dbConn = require('../plugins/db')

const admin = dbConn.define('admin', {
  nickName: {
    type: Sequelize.STRING,
    comment: '管理员昵称',
    validate: {
      notEmpty: {
        msg: '管理员昵称不能为空'
      },
    }
  },
  userName: {
    type: Sequelize.STRING,
    comment: '用户名',
    validate: {
      isText (value) {
        const reg = /\p{Unified_Ideograph}/ug
        if (reg.test(value)) {
          throw new Error('用户名不能为汉字')
        }
      },
    }
  },
  password: {
    type: Sequelize.STRING,
    comment: '密码',
    validate: {
      notEmpty: {
        msg: '密码不能为空'
      }
    },
    set (val) {
      if (val == '') return
      const bcrypt = require('bcryptjs')
      const salt = bcrypt.genSaltSync(10)
      this.setDataValue('password', bcrypt.hashSync(val, salt))
    }
  },
  avatar: {
    type: Sequelize.STRING(300),
    comment: '头像'
  },
  groupId: {
    type: Sequelize.BIGINT,
    comment: '分组'
  },
  lastLogin: {
    type: Sequelize.DATE,
    comment: '最后登录'
  },
  // lastLogin: {
  //   type: Sequelize.DATE,
  //   comment: '最后登录'
  // },
}, {
  tableName: 'SysAdmin',
  comment: '管理员'
})


module.exports = admin